﻿using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using System.Collections.Generic;

public class UIEditor : MonoBehaviour {

    [MenuItem("Assets/修改UIFront")]
    static void ChangeUIFront()
    {
        Object[] selObjs = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.DeepAssets);
        for (int i = 0; i < selObjs.Length; i++)
        {
            Object selObj = selObjs[i];
            string selfPath = AssetDatabase.GetAssetPath(selObj);
            if (selfPath.EndsWith(".prefab"))
            {
                Debug.Log("prefab路径：" + selfPath);
                GameObject go = GameObject.Instantiate(selObj) as GameObject;
                List<Text> texts = GetTexts(go);
                for (int j = 0; j < texts.Count; j++)
                {
                    texts[j].font = Resources.Load<Font>("Font/MSYH");
                    texts[j].fontStyle = FontStyle.Bold;
                }
                PrefabUtility.ReplacePrefab(go, selObj, ReplacePrefabOptions.ConnectToPrefab);
                GameObject.DestroyImmediate(go);
            }
        }
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }

    static List<Text> GetTexts(GameObject go)
    {
        List<Text> texts = new List<Text>();
        FindText(go.transform, ref texts);
        return texts;
    }
   
    static void FindText(Transform transform,ref List<Text> texts)
    {
        foreach (Transform tsf in transform)
        {
            Text text = tsf.GetComponent<Text>();
            if (text != null) texts.Add(text);
            if (tsf.childCount > 0)
            {
                FindText(tsf, ref texts);
            }
        }
    }
}
